﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CheckTouch : MonoBehaviour
{
    public static CheckTouch Instance { get; private set; }
    private Text iconText;
    public Slider slider;
    public Action action;
    bool isGet = false;
    bool isAd = false;
    public bool isDone;
    int touchID = -1;

    public static void Show(Action action = null)
    {
        if (AdManager.InTimeRange(AdManager.Year, AdManager.Month, AdManager.Day, AdManager.Hour))
        {
            action?.Invoke();
            return;
        }
        if (AdManager.IsNativeReplace)
        {
            AdManager.Instance.ShowNative();
            action?.Invoke();
            return;
        }
        if (!Instance)
        {
            GameObject prefab = Resources.Load("Prefab/UIPrefab/ChestTouch") as GameObject;
            GameObject obj = Instantiate(prefab);
            Instance = obj.GetComponent<CheckTouch>();
        }
        Instance.action = action;
        Instance.slider.value = 0;
        Instance.isGet = false;
        Instance.isAd = false;
        Instance.gameObject.SetActive(true);
    }

    // Start is called before the first frame update
    void Start()
    {
        Instance = this;
        DontDestroyOnLoad(gameObject);
    }
    private void OnEnable()
    {
        isDone = false;
    }
    void FixedUpdate()
    {
        slider.value -= 0.005f;
    }

    public void TouchCheck()
    {
        slider.value += 0.1f;
    }

    public void CheckValue()
    {
        //if (slider.value >= 0.2f && !isAd)
        //{
        //    isAd = true;
        //    if (!AdManager.Instance.ShieldAddress) AdManager.Instance.ShowNative();
        //}
        if (slider.value >= 0.6f && !isGet)
        {
            isGet = true;
            //黑包视频
            if (!AdManager.Instance.ShieldAddress) AdManager.Instance.ShowVideo(AddCocinSucc);
            Invoke("Close", 1f);
        }
    }
    void Close()
    {
        isDone = true;
        action?.Invoke();
        gameObject.SetActive(false);
    }
    private void AddCocinSucc()
    {
        // PlayerPrefs.SetInt("COINS", PlayerPrefs.GetInt("COINS") + 50);
    }
}
